/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.validator.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.form.client.validator.ValidatorCallback;

/**
 * the {@link ValidatorCallback} that call the
 * {@link AbstractSimpleValueValidator} for each value.
 */
class IterativeValidatorValidatorCallback
    implements ValidatorCallback
{

  /**
   * the original {@link ValidatorCallback} that is called at the end of the
   * computation.
   */
  private final ValidatorCallback originalValidatorCallback;

  /**
   * the {@link Iterator} that traverse each value.
   */
  private final Iterator<String> valuesIterator;

  /**
   * the current result of the validation.
   */
  private List<GwtI18nMessage> currentResult = new ArrayList<GwtI18nMessage>();

  /**
   * the {@link AbstractSimpleValueValidator} that is used on each value.
   */
  private AbstractSimpleValueValidator abstractSimpleValueValidator;



  /**
   * constructor.
   * 
   * @param abstractSimpleValueValidator
   *          the {@link AbstractSimpleValueValidator} that is used on each
   *          value
   * @param values
   *          the {@link List} of values of validate.
   * @param originalValidatorCallback
   *          the original {@link ValidatorCallback} that is called at the end
   *          of the computation.
   */
  IterativeValidatorValidatorCallback(AbstractSimpleValueValidator abstractSimpleValueValidator, List<String> values, ValidatorCallback originalValidatorCallback)
  {
    this.abstractSimpleValueValidator = abstractSimpleValueValidator;
    valuesIterator = values.iterator();
    this.originalValidatorCallback = originalValidatorCallback;
  }



  /**
   * this method is called after the validation has ended.
   * 
   * @param result
   *          is the result of the validation. It is <code>null</code> or an
   *          empty List when the value was valid and is the list of error
   *          messages as
   *          {@link com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage}
   *          when the value was invalid.
   * @see com.genia.toolbox.web.gwt.form.client.validator.ValidatorCallback#validationResult(java.util.List)
   */
  public void validationResult(List<GwtI18nMessage> result)
  {
    currentResult.addAll(result);
    validate();
  }



  /**
   * an action of validation on the current value.
   */
  void validate()
  {
    if (valuesIterator.hasNext()) {
      abstractSimpleValueValidator.validate(valuesIterator.next(), this);
    }
    else {
      originalValidatorCallback.validationResult(currentResult);
    }
  }

}
